#include "cashier.h"
#include "ui_cashier.h"

cashier::cashier(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::cashier),
    _listen(nullptr),
    _communtion(nullptr)
{
    ui->setupUi(this);
    _listen=new QTcpServer(this);               //服务器监听套接字
    _listen->listen(QHostAddress::Any,10001);   //监听
    connect(_listen,SIGNAL(newConnection()),this,SLOT(newClientConnect()));
    //connect(com,SIGNAL(readyread()),this,SLOT(readClientdata()));
//    model = new QStandardItemModel(this);
//    model->setColumnCount(2);
//    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);//设置选中时为整行选中
//    ui->tableView->setFocusPolicy(Qt::NoFocus);//取消焦点
//    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//设置无法编辑
//    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);//设置视图只能选择一个项目
//    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);//设置视图只能选择行

//    model->setHeaderData(0,Qt::Horizontal, "序号");
//    model->setHeaderData(1,Qt::Horizontal, "菜名");
//    ui->tableView->setModel(model);
}

cashier::~cashier()
{
    delete ui;
}

void cashier::newClientConnect()
{
    _communtion =_listen->nextPendingConnection();
    QString ip=_communtion->peerAddress().toString();
    qint16 port=_communtion->peerPort();
    QString temp=QString("[%1,%2]已连接").arg(ip).arg(port);
    qDebug()<<temp<<"客户端已经连接";
    //ui->textEdit->setText(temp);
    // mysql_init();
    connect(_communtion,SIGNAL(readyRead()),this,SLOT(readClientdata()));
}

void cashier::readClientdata()
{
    QByteArray array=_communtion->readAll();
    ui->textEdit->append(array);
}

void cashier::on_pushButton_clicked()
{
    emit mySignal();
}

void cashier::on_pushButton_2_clicked()
{
    int price=0;
    QString a=ui->lineEdit->text();     //拿到单号
    QString sql=QString("select price from people_%1").arg(a);
    QSqlQuery query;
    if(query.exec(sql))            //执行成功返回1
    {
        //QMessageBox::information(this,"人员管理","删除成功");
        statusBar()->showMessage("success"+sql);
        //ui->lineEdit->clear();
        //ui->lineEdit_2->clear();
    }
    else
    {
        statusBar()->showMessage(query.lastError().text());     //错误返回错误信息
    }
    for(int i=0;i<query.size();i++)
    {
        query.next();
        price+=query.value(0).toInt();
    }
    ui->lineEdit_2->setText(QString::number(price));
    QString sql2=QString("insert into doller (id,money) values(%1,%2)").arg(a).arg(price);
    QSqlQuery query1;
    query1.exec(sql2);
}

void cashier::on_pushButton_3_clicked()
{
    fp.resize(300,200);
    fp.setWindowTitle("账单");
    QLabel* b=new QLabel(&fp);
    b->setText("本次消费:");
    QTableWidget* c=new QTableWidget(&fp);
    load_zd(c);
    QSqlQuery query;
    QString sql=QString("drop table people_%1").arg(ui->lineEdit->text());
    query.exec(sql);
    fp.exec();
}
void cashier::load_zd(QTableWidget* c)
{
    c->clear();

    QString a=ui->lineEdit->text();
    QString sql=QString("select name,price from people_%1").arg(a);
    QSqlQuery query;
    if(query.exec(sql))            //执行成功返回1
    {
        //QMessageBox::information(this,"人员管理","删除成功");
        statusBar()->showMessage("success"+sql);
        //ui->lineEdit->clear();
        //ui->lineEdit_2->clear();
    }
    else
    {
        statusBar()->showMessage(query.lastError().text());     //错误返回错误信息
    }

    c->setRowCount(query.size());       //设置表格的行数
    c->setColumnCount(2);               //设置表格的列个数
    for(int i=0;i<query.size();i++)
    {
        query.next();
        for(int j=0;j<2;j++)
        {
            c->setItem(i,j,new QTableWidgetItem(query.value(j).toString()));
        }
     }
}

void cashier::on_pushButton_5_clicked()
{
   // model->clear();
    //model = new QStandardItemModel(this);
    QString a=ui->lineEdit_3->text();
    QString sql=QString("select name,price from people_%1 where over=0").arg(a);
    QSqlQuery query;
    query.exec(sql);

    QStringList fname;
    fname<<"菜名";
    fname<<"价格";
    ui->tableWidget->setRowCount(query.size());
    ui->tableWidget->setColumnCount(2);
    ui->tableWidget->setHorizontalHeaderLabels(fname);
    for(int i=0;i<query.size();i++)
    {
        query.next();
        for(int j=0;j<2;j++)
        {
                ui->tableWidget->setItem(i,j,new QTableWidgetItem(query.value(j).toString()));
        }
     }
//    for(int i=0;i<query.size();i++)
//    {
//        QList<QStandardItem*> list;
//        query.next();
//        list << new QStandardItem(QString::number(i)) << new QStandardItem(query.value(0).toString());
//       // model->insertRow(i, list);//在第0行插入一条记录
//    }
}

void cashier::on_pushButton_4_clicked()
{
    //int current=ui->tableView->
    QSqlQuery query;
    QString sql=QString("update people_%1 set over=1").arg(ui->lineEdit_3->text());
    query.exec(sql);
    int curRow=ui->tableWidget->currentRow();     //当前行号
    ui->tableWidget->removeRow(curRow);
}
